#coding=utf-8
#!/usr/bin/python
# 导入requests库
import requests
# 导入文件操作库
import os
import bs4
from bs4 import BeautifulSoup
import sys
import importlib
import random
import time
importlib.reload(sys)


# 越多越好
meizi_headers = [
    "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
    "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
    'Opera/9.25 (Windows NT 5.1; U; en)',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0",
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
]
# 给请求指定一个请求头来模拟chrome浏览器
global headers
headers = {'User-Agent': random.choice(meizi_headers)}
# 爬图地址
mziTu = 'https://fuliba2021.net'
# 定义存储位置
global save_path
save_path = 'E:\BeautifulPictures'


# 创建文件夹
def createFile(file_path):
    if os.path.exists(file_path) is False:
        os.makedirs(file_path)
    # 切换路径至上面创建的文件夹
    os.chdir(file_path)


# 下载文件
def download(page_no, file_path):
    global headers
    res_sub = requests.get(page_no, headers=headers)
    # 解析html
    soup_sub = BeautifulSoup(res_sub.text, 'html.parser')
    article = soup_sub.find('article',class_='article-content')
    if article is None:
        return
    all_img = article.find_all('img')
    print("共计图片" + str(len(all_img)))
    try:
        for imgHtml in all_img:
            headers = {'User-Agent': random.choice(meizi_headers)}
            # 提取图片地址
            url = imgHtml.attrs['src']
            img = requests.get(url, headers=headers)
            array = url.split('/')
            file_name = array[len(array) - 1]
            f = open(file_name, 'ab')
            f.write(img.content)
            f.close()
    except Exception as e:
        print(e)


# 格式化数字补齐三位   例如 1变为001
def format_num(num):
    if 0 < num < 10:
        return "00"+ str(num)
    if 9 < num < 100:
        return "0" + str(num)
    if 99 < num:
        return str(num)


# 主方法
def main():
    for q in range(1, 163):
        mzi_tu_q = mziTu + "/2021"+format_num(q)+".html"
        print("第几期:" + mzi_tu_q)
        res = requests.get(mzi_tu_q, headers=headers)
        # 使用自带的html.parser解析
        soup = BeautifulSoup(res.text, 'html.parser')
        # 创建文件夹
        createFile(save_path)
        # 获取首页总页数
        div = soup.find('div', class_='article-paging')
        if div is None:
            continue
        page = div.find_all('a')
        # img_max = page[len(page)-1].text
        # print("总页数:"+img_max)
        for i in range(1, len(page)+2):
            # 获取每页的URL地址
            if i == 1:
                continue
            else:
                page = mzi_tu_q + '/' + str(i)
            file = save_path + '\\' + str(i)
            createFile(file)
            # 下载每页的图片
            download(page, file)
    print("结束!")
    # 删掉已经挂了的图片


if __name__ == '__main__':
    main()